package ro.gillardon.persistence.document;

import ro.gillardon.persistence.ReservationDAO;
/**
 * Concrete RservationDAO
 *
 * @author haiurar
 */
import ro.gillardon.persistence.document.model.Reservation;
import ro.gillardon.persistence.exception.PersistenceException;

import com.orientechnologies.orient.core.db.document.ODatabaseDocumentPool;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.exception.OStorageException;
/**
 * Concrete ReservationDAO.
 *
 * @author haiurar
 */
public class DocumentReservationDAO extends AbstractApi implements ReservationDAO {

  /** The database connector. */
  private final ODatabaseDocumentTx db;

  /**
   * Instantiates a new DocumentReservationDAO.
   */
  public DocumentReservationDAO() {
    this.db = ODatabaseDocumentPool.global().acquire(DATABASE_URL, USERNAME, PASSWORD);
  }

  @Override
  public Reservation save(final Reservation reservation) throws PersistenceException {
    try {
      reservation.getDoc().save();
      return reservation;
    } catch (OStorageException | ClassCastException e) {
      throw new PersistenceException(e.getMessage(), e);
    } finally {
      this.db.close();
    }
  }

}
